package com.ssu.judea.travlog.model;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import statics.TravlogVars;

import com.ssu.judea.travlog.vo.DatePost;
import com.ssu.judea.travlog.vo.Member;
import com.ssu.judea.travlog.vo.ResultPost;
import com.ssu.judea.travlog.vo.ResultRecentPost;

import comm.util.SqlMapLoader;
import comm.util.TravlogHashMap;
import comm.util.TwitterAPI;

public class WebDAO extends TravlogDAO {
	public void toAuthpage(HttpServletRequest request,HttpServletResponse response){
		TwitterAPI twit = new TwitterAPI();
		twit.toAuthPage(request, response);
	}
	
	public ArrayList<ResultRecentPost> getRecentOpenPosts() {
		ArrayList<ResultRecentPost> resultList = null;
		
		try {
			resultList = (ArrayList<ResultRecentPost>)sqlMapper.queryForList("post.getRecentOpenPosts");
		} catch (SQLException e) {
			logger.debug(e.getMessage());
			return null;
		}
		
		return resultList;
	}
	
	public ArrayList<DatePost> getUserPostDateList(String memberseq) {
		ArrayList<DatePost> resultList = null;
		try {
			resultList = (ArrayList<DatePost>)sqlMapper.queryForList("post.getUserPostDateList", memberseq);
		} catch (SQLException e) {
			return null;
		}
		return resultList;
		
	}
	
	public ArrayList<ResultPost> getUserPostByDate(String serverUrl,String memberseq, String targetdate) {
		TravlogHashMap paramHm = new TravlogHashMap();
		paramHm.put("MEMBER_SEQ", memberseq);
		paramHm.put("TARGET_DATE", targetdate);
		
		ArrayList<ResultPost> resultList = null;
		
		try {
			resultList = (ArrayList<ResultPost>)sqlMapper.queryForList("post.getUserPostByDate", paramHm);
		} catch (SQLException e) {
			return null;
		}
		return resultList;
	}
	
	public ArrayList<DatePost> getFollowersList(String curClientSeq) {
		ArrayList<Member> list = null;
		ArrayList<DatePost> datePostList = new ArrayList<DatePost>();
		
		if(sqlMapper==null)sqlMapper = SqlMapLoader.getInstance();
		
		Member mem = getMemberBySeq(curClientSeq);
		
		String passkey = mem.getTwitterpasskey();
		
		//트윗 follow 처리
		if(twit==null)twit = new TwitterAPI();
		ArrayList<Integer> followersIds = null;
		if(passkey.length()>0)
			followersIds = twit.getMyFollowersList(passkey);
		
		if(followersIds!=null && followersIds.size()>0){
			list = new ArrayList<Member>();
			for(int i = 0; i<followersIds.size();i++){
				Member currentItem = getMemberByNumber(followersIds.get(i).toString());
				
				if(currentItem!=null)list.add(currentItem);
			}
			
			logger.debug("getFollowersInfo Result"+list.size());
			
			if(list.size()>0){
				
				for(int i = 0; i<list.size();i++){
					datePostList.addAll(getUserPostDateListArrayList(String.valueOf(list.get(i).getMemberseq())));					
				}
			}
		}
		
		return datePostList;
		
	}
}
